package com.hx.voice.recognition.common.util;

import com.hx.voice.recognition.common.vo.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @descripe：双重循环封装父子菜单
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/19 10:54
 * @version：V1.0
 */
public class TreeUtil {

    /**
     * @description 双重循环判断父子关系
     * @method  build
     * @param  [treeNode, root]
     * @return java.util.List<T>
     * @date: 2019/6/19 11:00
     * @author:zhoujinbing
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes, Object root)
    {
        List<T> trees = new ArrayList<T>();

        for (T treeNode : treeNodes) {

            if (root.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }

            for (T it : treeNodes) {
                if (it.getParentId() == treeNode.getId()) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<TreeNode>());

                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }



}